#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>

//int main()
//{
//  int i = 0;
//  for (; i< 134; i++)
//  {
//    printf("%d->%s\n", i, strerror(i));
//  }
//  
//  FILE* fp = fopen("log.txt", "r");
//  if (fp == NULL) return errno;
//
//
//  return 0;
//}

void print_with_timestamp(const char *message)
{
  time_t now;
  char timestamp[20];
  time(&now);
  strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", localtime(&now));
  printf("[%s] %s\n", timestamp, message);
}

int main()
{
  pid_t pid;

  pid  = fork();

  if (pid < 0)
  {
    fprintf(stderr, "fork failed\n");
    return 1;
  }
  else if (pid == 0)
  {
    print_with_timestamp("This is the child process");
    for (int i = 1; i <= 5; i++)
    {
      sleep(1);
      print_with_timestamp("Child process working...");
    }
    print_with_timestamp("child process finished.");
  }
  else
  {
    print_with_timestamp("This is the parent process");
    for (int i = 1; i <= 5; ++i)
    {
      sleep(1);
      print_with_timestamp("Parent process working.");
    }
    print_with_timestamp("Parent process finished."); 
  
    wait(NULL);
  print_with_timestamp("Parent process has waited for child to finish");
  }

}


